package com.yk.test.config.oauth2;

import com.yk.test.servcies.MyClientDetailsServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.client.BaseClientDetails;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class MyClientDetailsService implements ClientDetailsService {

    @Autowired
    private MyClientDetailsServices myClientDetailsServices;

    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        com.yk.test.entity.ClientDetails clientDetails =  myClientDetailsServices.selectByClientId(clientId);
        BaseClientDetails baseClientDetails = new BaseClientDetails();
        baseClientDetails.setClientId(clientDetails.getClientId());
        baseClientDetails.setClientSecret(new BCryptPasswordEncoder().encode(clientDetails.getClientSecret()));
        baseClientDetails.setScope(Arrays.asList("all"));
        baseClientDetails.setAuthorizedGrantTypes(Arrays.asList("refresh_token", "password", "authorization_code","client_credentials"));
        baseClientDetails.setAccessTokenValiditySeconds(RedisTokenUtil.REDIS_TOKEN_TIME);
        baseClientDetails.setRefreshTokenValiditySeconds(RedisTokenUtil.REDIS_REFRESH_TOKEN_TIME);
       /* List<GrantedAuthority> authorities = new ArrayList<>();
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("test");
        authorities.add(grantedAuthority);
        baseClientDetails.setAuthorities(authorities);*/
        return baseClientDetails;
    }
}
